// pages/orders/index.js
import { getOrderList } from "../../service/index.js"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dataType: 'all',
    active: null,
    page: 1,
    goodsList: [],
    buttonText: ''
  },

  onLoad(options) {
    console.log(options);
    if (options.type) {
      this.setData({
        dataType: options.type
      })
    }
    console.log(this.data.dataType);
    this.getOrderList()
  },
  // 获取订单
  getOrderList: function () {
    const dataType = this.data.dataType
    const page = this.data.page
    getOrderList(dataType, page, (res) => {
      // console.log(res, 1111111111111);
      const { data } = res.data.list
      const state_text = data.state_text
      // console.log(data, '商品列表');
      this.setData({
        goodsList: data
      })
      if(state_text === 'payment' || state_text === 'delivery'){
        this.setData({
          buttonText:'申请取消'
        })
      }else if(state_text === 'received'){
        this.setData({
          buttonText:'确认收货'
        })
      }else{
        this.setData({
          buttonText:'评价'
        })
      }
    })
  },
  // 计算订单中商品的总数量
  calculateOrderTotalCount: function (goods) {
    let total = 0;
    for (let item of goods) {
      total += item.total_num;
    }
    return total;
  },
  // 处理 tab 点击事件，切换 dataType
  onTabChange: function (event) {
    const dataType = event.detail.name;
    this.setData({ dataType: dataType }, () => {
      this.getOrderList();
    });

  },
  // 跳转到订单详情
  toOrderPage(event){
    const order= event.currentTarget.dataset.item
    wx.navigateTo({
      url:`/pages/orderDetail/index?order=${JSON.stringify(order)}`
    })
  }
})
